﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Windows.Forms;

namespace XmlAppliction.Doman
{
    public class ConfigUser
    {
        string xmlPath = "";
        private DataSet configDs;//配置列表

        public ConfigUser(string xmlPath)
        {
            this.xmlPath = xmlPath;
            configDs = new DataSet();
            configDs.ReadXml(xmlPath);
            try
            {
                //读取链接字符串解密字符串
                configDs.Tables[0].Rows[0]["connectionString"] = DBUtility.DESEncrypt.Decrypt(configDs.Tables[0].Rows[0]["connectionString"].ToString());
            }
            catch
            {
                throw new Exception("数据库连接配置错误！");
            }
        }
        public string ConnectionString
        { 
            get { return GetValue("connectionString"); }
            set { SetValue("connectionString",value) ; }
        }
        public string SaveXMLPath
        { 
            get { return GetValue("saveXMLPath"); }
            set { SetValue("saveXMLPath",value) ; }
        }
        public string LastsaveXMLPath
        { 
            get { return GetValue("lastsaveXMLPath"); }
            set { SetValue("lastsaveXMLPath",value) ; }
        }
        public string WordMubanPath
        { 
            get { return GetValue("wordMubanPath"); }
            set { SetValue("wordMubanPath",value) ; }
        }
        public string SaveWordPath
        { 
            get { return GetValue("saveWordPath"); }
            set { SetValue("saveWordPath",value) ; }
        }  
        public string TreeCondition
        { 
            get { return GetValue("treeCondition"); }
            set { SetValue("treeCondition",value) ; }
        }
        public string SSTJCondition
        {
            get { return GetValue("SSTJCondition"); }
            set { SetValue("SSTJCondition", value); }
        }
        public string 统计项名称
        { 
            get { return GetValue("统计项名称"); }
            set { SetValue("统计项名称",value) ; }
        } 
        public string 统计项编号
        { 
            get { return GetValue("统计项编号"); }
            set { SetValue("统计项编号",value) ; }
        } 
        public string 执行语句
        { 
            get { return GetValue("执行语句"); }
            set { SetValue("执行语句",value) ; }
        } 
        public string 是否显示
        { 
            get { return GetValue("是否显示"); }
            set { SetValue("是否显示",value) ; }
        } 
        public string 执行状态
        { 
            get { return GetValue("执行状态"); }
            set { SetValue("执行状态",value) ; }
        } 
        public string 类型
        { 
            get { return GetValue("类型"); }
            set { SetValue("类型",value) ; }
        } 
        public string 结果
        { 
            get { return GetValue("结果"); }
            set { SetValue("结果",value) ; }
        } 
        public string 统计描述
        { 
            get { return GetValue("统计描述"); }
            set { SetValue("统计描述",value) ; }
        } 
        public string 连接参数编号
        { 
            get { return GetValue("连接参数编号"); }
            set { SetValue("连接参数编号",value) ; }
        } 
        public string 连接参数
        {
            get { return GetValue("连接参数"); }
            set { SetValue("连接参数",value) ; }
        }   

        public string  GetValue(string filterName)
        {
            try
            {
                return configDs.Tables[0].Rows[0][filterName].ToString();
            }
            catch
            {
                return null;
            }
        }
        public void SetValue(string filterName,string value)
        {
            try
            {
                configDs.Tables[0].Rows[0][filterName] = value;
            }
            catch(Exception ex)
            {
                throw ex;
            }
        }

        public void saveConfigXML()
        {
            try
            {
                try
                {
                    //保存链接字符串加盟字符串
                    configDs.Tables[0].Rows[0]["connectionString"] = DBUtility.DESEncrypt.Encrypt(configDs.Tables[0].Rows[0]["connectionString"].ToString());
                }
                catch
                {
                    throw new Exception("数据库连接配置错误！");
                }
                configDs.WriteXml(xmlPath);
                try
                {
                    //保密数据后解密字符串
                    configDs.Tables[0].Rows[0]["connectionString"] = DBUtility.DESEncrypt.Decrypt(configDs.Tables[0].Rows[0]["connectionString"].ToString());
                }
                catch
                {
                    throw new Exception("数据库连接配置错误！");
                }
            }
            catch
            {
                MessageBox.Show(string.Format("无权操作配置目录({0})请以管理员身份运行！", xmlPath), "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
